// //查看学员课程评价及回复


import React, { useEffect, useState } from 'react';
import { Layout, List, Avatar, Form, Button, Input, message } from 'antd';
import { getStudentCourseMessageAPI,sentStudentCourseMessageAPI } from '@/api/coach';
import { getuserName } from '@/utils';

const { Header, Content } = Layout;
const { TextArea } = Input;

const CoachFeedback = () => {
  // 假设这是学员对课程评价的数据
  const studentFeedbackData = [
    {
      id: 1,
      student: '学员1',
      message: '课程内容很丰富，收获颇丰。',
      timestamp: new Date(),
      replyContent: '', // 追加回复内容的状态
    },
    {
      id: 2,
      student: '学员2',
      message: '希望能有更多的实践机会。',
      timestamp: new Date(),
      replyContent: '', // 追加回复内容的状态
    },
    // 更多评价数据...
  ];
  //存储留言列表
  const [studentFeedback, setStudentFeedback] = useState([]);
  //存储回复信息
  const [coachReply,setCoachReply] = useState('')
  useEffect(()=>{
    async function getStudentCourseMessage(){
      const res = await getStudentCourseMessageAPI(getuserName())
      console.log(res.data)
      if(res !== '')
      {
        const filterRes = res.data.filter(item =>item.coachIsReplied === 0)
        setStudentFeedback(filterRes)
      }
      
    }
    getStudentCourseMessage()
  },[])

  // 处理回复提交事件
  const handleReplySubmit = async(item) => {
    console.log(item.evaluateId)
    const evaluateId =item.evaluateId
    if(coachReply ==='')
      {
        message.warning('请输入回复信息！')
      }
      else{
        await sentStudentCourseMessageAPI({
          evaluateId,
          coachReply,
          coachIsReplied:1
        })
        message.success('回复发送成功！')
      }
    // const updatedFeedback = studentFeedback.map((item) => {
    //   if (item.evaluateId === feedbackId && item.replyContent.trim() !== '') {
    //     message.success('回复发送成功！');
    //     return { ...item, replyContent: '' };
    //   }
    //   return item;
    // });

    // setStudentFeedback(updatedFeedback);
  };
 const handleTextArea =(e)=>{
  console.log(e)
  setCoachReply(e)
  // setStudentFeedback((prevFeedback) =>
  //   prevFeedback.map((prevItem) =>
  //     prevItem.id === item.evaluateId ? { ...prevItem, replyContent: e.target.value } : prevItem
  //   )
  // )
 }

  return (
    <Layout>
      <Header style={{ background: '#fff', textAlign: 'center' }}>
        <h1 style={{ fontSize: '1.5rem' }}>查看学员课程评价及回复</h1>
      </Header>
      <Content style={{ padding: '20px' }}>
        <List
          itemLayout="horizontal"
          dataSource={studentFeedback}
          renderItem={(item) => (
            <List.Item key={item.evaluateId}>
              <List.Item.Meta
                avatar={<Avatar src={`https://i.pravatar.cc/150?u=${item.student}`} />}
                title={<a>{item.studentName}</a>}
                description={
                  <p>
                    <p>{item.businessName}</p>
                  <p>{item.studentEvaluateContent}</p>
                  </p>
              }
              />
              <Form.Item>
                <TextArea
                  rows={4}
                  value={item.replyContent}
                  onChange={(e) =>handleTextArea(e.target.value)
                  }
                />
              </Form.Item>
              <Form.Item>
                <Button
                  htmlType="submit"
                  onClick={() => handleReplySubmit(item)}
                  type="primary"
                >
                  回复
                </Button>
              </Form.Item>
            </List.Item>
          )}
        />
      </Content>
    </Layout>
  );
};

export default CoachFeedback;
